<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use App\Controller\AbstractController;
use Hyperf\Di\Annotation\Inject;
use App\Utils\ApiUtils;
use App\Service\LoginService;
use App\Constants\ErrorCode;
use App\Validate\AdminValidation;


class LoginController extends AbstractController
{

    /**
     * @Inject 
     * @var LoginService
     */
    protected $service;


    //验证码
    public function verify()
    {
        $result = $this->service->verify();
        return $this->response->withCookie($result['cookie'])->withAddedHeader('content-type', 'image/png; charset=utf-8')
            ->withContent($result['content']);
    }

    //登录接口
    public function login()
    {
        //从cookie中那验证码
        $cookie_code = $this->request->cookie('phrase_code', '');
        $data['name'] = $this->request->input('name');
        $data['password'] = $this->request->input('password');
        $data['code'] = $this->request->input('code');
        make(AdminValidation::class)->check($data,'login')->response();
        $result = $this->service->login($data,$cookie_code);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'登录成功',$result));
      
    }

    //刷新token
    public function refreshToken()
    {
        $header = $this->request->getHeader('authorization');
        return $this->service->refreshToken($header);

    }
}
